home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / eckelt01.zip / 13 / WIND4.CPP < prev    next >
C/C++ Source or Header  |  1995-02-23  |  3KB  |  122 lines

  1. // File from page 538 in "Thinking in C++" by Bruce Eckel
  2. //////////////////////////////////////////////////
  3. // From the compressed package ECKELT01.ZIP 2/21/95
  4. // Copyright (c) Bruce Eckel, 1995 
  5. // Source code file from the book "Thinking in C++", 
  6. // Prentice Hall, 1995, ISBN: 0-13-917709-4
  7. // All rights reserved EXCEPT as allowed by the following 
  8. // statements: You may freely use this file for your own 
  9. // work, including modifications and distribution in 
  10. // executable form only. You may copy and distribute this 
  11. // file, as long as it is only distributed in the complete 
  12. // (compressed) package with the other files from this 
  13. // book and you do not remove this copyright and notice. 
  14. // You may not distribute modified versions of the source 
  15. // code in this package. This package may be freely placed 
  16. // on bulletin boards, internet nodes, shareware disks and 
  17. // product vendor disks. You may not use this file in 
  18. // printed media without the express permission of the 
  19. // author. Bruce Eckel makes no 
  20. // representation about the suitability of this software 
  21. // for any purpose. It is provided "as is" without express 
  22. // or implied warranty of any kind. The entire risk as to 
  23. // the quality and performance of the software is with 
  24. // you. Should the software prove defective, you assume 
  25. // the cost of all necessary servicing, repair, or 
  26. // correction. 
  27. // If you think you've found an error, please 
  28. // email all modified files with loudly commented changes 
  29. // to: eckel@aol.com (please use the same 
  30. // address for non-code errors found in the book).
  31. //////////////////////////////////////////////////
  32.  
  33. //: WIND4.CPP -- Extensibility in OOP
  34. #include <iostream.h>
  35. enum note { middleC, Csharp, Cflat }; // Etc.
  36.  
  37. class instrument {
  38. public:
  39.   virtual void play(note) const {
  40.     cout << "instrument::play" << endl;
  41.   }
  42.   virtual char* what() const {
  43.     return "instrument";
  44.   }
  45.   // Assume this will modify the object:
  46.   virtual void adjust(int) {}
  47. };
  48.  
  49. class wind : public instrument {
  50. public:
  51.   void play(note) const {
  52.     cout << "wind::play" << endl;
  53.   }
  54.   char* what() const { return "wind"; }
  55.   void adjust(int) {}
  56. };
  57.  
  58. class percussion : public instrument {
  59. public:
  60.   void play(note) const {
  61.     cout << "percussion::play" << endl;
  62.   }
  63.   char* what() const { return "percussion"; }
  64.   void adjust(int) {}
  65. };
  66.  
  67. class string : public instrument {
  68. public:
  69.   void play(note) const {
  70.     cout << "string::play" << endl;
  71.   }
  72.   char* what() const { return "string"; }
  73.   void adjust(int) {}
  74. };
  75.  
  76. class brass : public wind {
  77. public:
  78.   void play(note) const {
  79.     cout << "brass::play" << endl;
  80.   }
  81.   char* what() const { return "brass"; }
  82. };
  83.  
  84. class woodwind : public wind {
  85. public:
  86.   void play(note) const {
  87.     cout << "woodwind::play" << endl;
  88.   }
  89.   char* what() const { return "woodwind"; }
  90. };
  91.  
  92. // Identical function from before:
  93. void tune(instrument& i) {
  94.   // ...
  95.   i.play(middleC);
  96. }
  97.  
  98. // New function:
  99. void f(instrument& i) { i.adjust(1); }
  100.  
  101. // Upcasting during array initialization:
  102. instrument* A[] = {
  103.   new wind,
  104.   new percussion,
  105.   new string,
  106.   new brass
  107. };
  108.  
  109. main() {
  110.   wind flute;
  111.   percussion drum;
  112.   string violin;
  113.   brass flugelhorn;
  114.   woodwind recorder;
  115.   tune(flute);
  116.   tune(drum);
  117.   tune(violin);
  118.   tune(flugelhorn);
  119.   tune(recorder);
  120.   f(flugelhorn);
  121. }
  122.